package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @GetMapping("/setmeal/page")
    public ResultInfo save(
            String name,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<Setmeal> page = setmealService.findByPage(name, pageNum, pageSize);
        return ResultInfo.success(page);
    }
    @CacheEvict(value = "setmeal", key = "#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal) {
        setmealService.save(setmeal);
        return ResultInfo.success(null);
    }
    @CacheEvict(value = "setmeal", allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo delete(Long[] ids) {
        if (ids != null && ids.length > 0) {
            setmealService.deleteById(ids);
        }
        return ResultInfo.success(null);
    }
    @CacheEvict(value = "setmeal", allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateByStatus(
            @PathVariable("status") Integer status,
            @RequestParam List<Long> ids
    ) {
        setmealService.updateByStatus(status,ids);
        return ResultInfo.success(null);
    }

    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        Setmeal setmeal=setmealService.findById(id);
        return ResultInfo.success(setmeal);

    }
    @CacheEvict(value = "setmeal", allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo updateById(@RequestBody Setmeal setmeal){
        setmealService.updateById(setmeal);
        return ResultInfo.success(null);
    }

}
